<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入视频标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="画质" prop="quality">
        <el-select v-model="queryParams.quality" placeholder="请选择画质" clearable>
          <el-option label="4K超清" value="4K" />
          <el-option label="高清HD" value="HD" />
          <el-option label="标清SD" value="SD" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="草稿" value="draft" />
          <el-option label="审核中" value="review" />
          <el-option label="已发布" value="published" />
          <el-option label="已下架" value="offline" />
        </el-select>
      </el-form-item>
      <el-form-item label="上传时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['video:content:add']"
        >上传视频</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="handleBatchPublish"
          v-hasPermi="['video:content:edit']"
        >批量发布</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="handleBatchOffline"
          v-hasPermi="['video:content:edit']"
        >批量下架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['video:content:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="视频ID" align="center" prop="videoId" width="70" />
      <el-table-column label="封面" align="center" width="120">
        <template slot-scope="scope">
          <div style="width: 100px; height: 56px; background: linear-gradient(135deg, #667eea, #764ba2); border-radius: 4px; display: flex; align-items: center; justify-content: center; color: white;">
            <i class="el-icon-video-play" style="font-size: 24px;"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="视频信息" align="left" min-width="200">
        <template slot-scope="scope">
          <div>
            <div style="font-weight: bold; margin-bottom: 5px;">{{ scope.row.title }}</div>
            <div style="font-size: 12px; color: #909399;">
              <el-tag v-if="scope.row.quality === '4K'" type="danger" size="mini">4K</el-tag>
              <el-tag v-else-if="scope.row.quality === 'HD'" type="success" size="mini">HD</el-tag>
              <el-tag v-else type="info" size="mini">SD</el-tag>
              <span style="margin-left: 8px;">⏱️ {{ scope.row.duration }}</span>
              <span style="margin-left: 8px;">📅 {{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.price == 0" style="color: #67C23A; font-weight: bold;">免费</div>
          <div v-else>
            <div style="color: #f56c6c; font-weight: bold; font-size: 16px;">¥{{ scope.row.price }}</div>
            <div v-if="scope.row.originalPrice" style="color: #909399; font-size: 12px; text-decoration: line-through;">¥{{ scope.row.originalPrice }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="播放量" align="center" width="100">
        <template slot-scope="scope">
          {{ formatNumber(scope.row.playCount) }}
        </template>
      </el-table-column>
      <el-table-column label="收入" align="center" width="100">
        <template slot-scope="scope">
          <span style="color: #f56c6c; font-weight: bold;">¥{{ scope.row.totalRevenue || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'published'" type="success">已发布</el-tag>
          <el-tag v-else-if="scope.row.status === 'draft'" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === 'review'" type="warning">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === 'offline'" type="danger">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:content:edit']"
          >编辑</el-button>
          <el-button
            v-if="scope.row.status === 'draft' || scope.row.status === 'offline'"
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handlePublish(scope.row)"
            v-hasPermi="['video:content:edit']"
          >发布</el-button>
          <el-button
            v-if="scope.row.status === 'published'"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleOffline(scope.row)"
            v-hasPermi="['video:content:edit']"
          >下架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['video:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入视频标题" maxlength="200" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="视频画质" prop="quality">
              <el-select v-model="form.quality" placeholder="请选择画质">
                <el-option label="4K超清" value="4K" />
                <el-option label="高清HD" value="HD" />
                <el-option label="标清SD" value="SD" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长" prop="duration">
              <el-input v-model="form.duration" placeholder="例如: 142:30 或 02:22:30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格(元)" prop="price">
              <el-input-number v-model="form.price" :precision="2" :step="0.01" :min="0" :max="999.99" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原价(元)" prop="originalPrice">
              <el-input-number v-model="form.originalPrice" :precision="2" :step="0.01" :min="0" :max="999.99" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="视频描述" prop="description">
              <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入视频描述" maxlength="500" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="视频URL" prop="videoUrl">
              <el-input v-model="form.videoUrl" placeholder="请输入视频URL" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="封面URL" prop="coverUrl">
              <el-input v-model="form.coverUrl" placeholder="请输入封面URL" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标签" prop="tags">
              <el-input v-model="form.tags" placeholder="多个标签用逗号分隔，例如：动作,飙车,好莱坞" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option label="草稿" value="draft" />
                <el-option label="审核中" value="review" />
                <el-option label="已发布" value="published" />
                <el-option label="已下架" value="offline" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVideoInfo, getVideoInfo, delVideoInfo, addVideoInfo, updateVideoInfo, batchPublish, batchOffline } from "@/api/video/content";

export default {
  name: "VideoContent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频表格数据
      videoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        quality: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "视频标题不能为空", trigger: "blur" }
        ],
        quality: [
          { required: true, message: "画质不能为空", trigger: "change" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视频列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (this.dateRange != null && this.dateRange.length > 0) {
        this.queryParams.params["beginTime"] = this.dateRange[0];
        this.queryParams.params["endTime"] = this.dateRange[1];
      }
      listVideoInfo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 格式化数字 */
    formatNumber(num) {
      if (!num) return 0;
      if (num >= 10000) {
        return (num / 10000).toFixed(1) + '万';
      }
      return num;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        videoId: null,
        title: null,
        description: null,
        videoUrl: null,
        coverUrl: null,
        duration: null,
        quality: "HD",
        price: 0.00,
        originalPrice: null,
        status: "draft",
        tags: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.videoId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "上传视频";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const videoId = row.videoId || this.ids
      getVideoInfo(videoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改视频信息";
      });
    },
    /** 发布视频 */
    handlePublish(row) {
      const videoIds = [row.videoId];
      this.$modal.confirm('是否确认发布视频"' + row.title + '"？').then(function() {
        return batchPublish(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发布成功");
      }).catch(() => {});
    },
    /** 下架视频 */
    handleOffline(row) {
      const videoIds = [row.videoId];
      this.$modal.confirm('是否确认下架视频"' + row.title + '"？').then(function() {
        return batchOffline(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => {});
    },
    /** 批量发布 */
    handleBatchPublish() {
      const videoIds = this.ids;
      this.$modal.confirm('是否确认批量发布选中的' + videoIds.length + '个视频？').then(function() {
        return batchPublish(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发布成功");
      }).catch(() => {});
    },
    /** 批量下架 */
    handleBatchOffline() {
      const videoIds = this.ids;
      this.$modal.confirm('是否确认批量下架选中的' + videoIds.length + '个视频？').then(function() {
        return batchOffline(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.videoId != null) {
            updateVideoInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideoInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const videoIds = row.videoId || this.ids;
      this.$modal.confirm('是否确认删除视频编号为"' + videoIds + '"的数据项？').then(function() {
        return delVideoInfo(videoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
</style>

